<?php

/**
 * resource actions.
 *
 * @package    travide
 * @subpackage resource
 * @author     Travide Project Group
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class resourceActions extends sfActions {

    public function executeDownloadResource(sfWebRequest $request) {
        $this->forward404Unless($collectionResource = Doctrine::getTable('CollectionResource')->find($request->getParameter('uin')));
        if ($collectionResource->getRequiresIngest()) {

            /**
             * Work around for ORACLE repositories where resources currently
             * can't be ingested - to be fixed shortly
             */
            $resourceFilename = Doctrine::getTable('CollectionResource')->getResourceUploadDir().DIRECTORY_SEPARATOR.$collectionResource->getResourceLink();
            if (sfConfig::get('app_resources_allow_non_ingested_download') && (file_exists($resourceFilename)) ) {

                $this->setLayout(false);

                $response = $this->getResponse();
                $response->setContentType('application/octet-stream');
                $response->setHttpHeader('Content-Disposition', 'attachement; filename=' . urlencode($collectionResource->getOriginalFilename()) );
                $response->addCacheControlHttpHeader('no-cache');
                return $this->renderText(file_get_contents($resourceFilename));
            } else {
                $this->setTemplate('resourceRequiresIngest');
            }
        } else {

            $this->setLayout(false);

            $response = $this->getResponse();
            $response->setContentType('application/octet-stream');
            $response->setHttpHeader('Content-Disposition', 'attachement; filename=' . $collectionResource->getOriginalFilename());
            $response->addCacheControlHttpHeader('no-cache');

            $resourceData = stream_get_contents($collectionResource->getResourceData());
            if ($collectionResource->getBase64Encoded()) {
                $resourceData = base64_decode($resourceData);
            }
            return $this->renderText($resourceData);
        }
    }

}
